package com.simpleaccount.Search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;

import com.simpleaccount.Common.DateUtils;
import com.simpleaccount.Common.MySpinnerAdapter;
import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Category;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/14.
 */

public class OutcomeFragment_search extends Fragment {
    @Bind(R.id.lv_search_2)
    ListView lvSearch2;
    @Bind(R.id.spinner_search_pay)
    Spinner spinnerSearchPay;
    @Bind(R.id.spinner_search_year)
    Spinner spinnerSearchYear;
    @Bind(R.id.spinner_search_month)
    Spinner spinnerSearchMonth;
    private Bundle bundle;
    private List<Entry> entries;
    private MyListViewAdapter adapter;
    private AdapterView.OnItemSelectedListener listener=new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            adapter.setData(searchByCondition());
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle=getArguments();

    }
    public List<String> getData() {
        List<String> list = new ArrayList<>();
        list.add("全部");
        for (Category e:DbUtils.getCategory(0)) {
            list.add(e.getName());
        }
        return list;
    }
    public List<String> getDatayear() {
        List<String> list = new ArrayList<>();
        Time time =new Time();
        time.setToNow();
        int year = time.year;
        for(int i=0;i<=10;i++){

            list.add(String.valueOf(year-i)+"年");
        }
        return list;
    }
    public List<String> getDatamonth() {
        List<String> list = new ArrayList<>();
        for(int i=1;i<=12;i++){

            list.add(String.valueOf(i)+"月");
        }
        return list;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_outcome, container, false);
        ButterKnife.bind(this, view);
        adapter=new MyListViewAdapter(searchByCondition(), getContext());
        lvSearch2.setAdapter(adapter);
        Time time=new Time();
        time.setToNow();
        int year=time.year-2015;
        int month=time.month;
        MySpinnerAdapter adapter = new MySpinnerAdapter(getActivity(), getData());
        spinnerSearchPay.setAdapter(adapter);
        MySpinnerAdapter adapteryear = new MySpinnerAdapter(getActivity(), getDatayear());
        spinnerSearchYear.setAdapter(adapteryear);
        MySpinnerAdapter adaptermonth = new MySpinnerAdapter(getActivity(), getDatamonth());
        spinnerSearchMonth.setAdapter(adaptermonth);
        spinnerSearchMonth.setSelection(month,true);
//        spinnerSearchYear.setSelection(year,true);

        spinnerSearchMonth.setOnItemSelectedListener(listener);
        spinnerSearchYear.setOnItemSelectedListener(listener);
        spinnerSearchPay.setOnItemSelectedListener(listener);

        return view;

    }

    public OutcomeFragment_search newInstance() {
        OutcomeFragment_search myFragment = new OutcomeFragment_search();
        Bundle args = new Bundle();
        myFragment.setArguments(args);
        return myFragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private List<Entry> searchByCondition() {
        String category = (String) spinnerSearchPay.getSelectedItem();
        String year = (String)spinnerSearchYear.getSelectedItem();
        String month = (String)spinnerSearchMonth.getSelectedItem();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy年M月");
        Date date=new Date();
        Calendar calendar=Calendar.getInstance();
        try {
            date=sdf.parse(year+month);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        if(category.equals("全部")){
            entries=DbUtils.queryByDate(DateUtils.getTimesMonthmorning(calendar),DateUtils.getTimesMonthnight(calendar));
        }else {
            entries = DbUtils.queryByCategoryAndDate(category,DateUtils.getTimesMonthmorning(calendar),DateUtils.getTimesMonthnight(calendar));
        }
        List<Entry> entries1=new ArrayList<>();
        for(Entry entry:entries){
            if(entry.getType()==0)
                entries1.add(entry);
        }
        return entries1;
    }
    public void refresh(){
        adapter.setData(searchByCondition());
        adapter.notifyDataSetChanged();
    }
}
